﻿Imports Entities
Imports Business
Imports DataAccess
Public Class MantencionFormasPago
    Private _FPago As New FORMAPAGOS
    Private _FPagosList As New List(Of formapagos)

    Private Sub MantencionFormasPago_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        Limpiar()
        Titulo()
    End Sub

    Private Sub MantencionFormasPago_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
    End Sub
#Region "Métodos Privados"
    Private Sub Titulo()
        _FPagosList = FormaPagoBL.List
        With lsFormasPago
            .DataSource = _FPagosList
            .DisplayMember = "DESCRIPCION"
            .ValueMember = "FORMAPAGO"
        End With
    End Sub

    Private Sub Limpiar()
        _FPago = New FORMAPAGOS
        tDescripcion.Clear()
        bGuardar.Text = "Guardar"

        bEliminar.Enabled = False
    End Sub

    Private Sub cargarFPago()
        If IsNothing(_FPago) Then
            Limpiar()
        Else
            With _FPago
                tDescripcion.Text = _FPago.DESCRIPCION
            End With

            bGuardar.Text = "Modificar"
            bEliminar.Enabled = True
        End If
    End Sub
#End Region

    Private Sub bGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bGuardar.Click
        If tDescripcion.Text = "" Then
            Util.Mensaje("Debe Ingresar una Descripción de Camión")
            tDescripcion.Focus()
            Exit Sub
        End If

        Dim fpago As New FORMAPAGOS
        With fpago
            .FORMAPAGO = _FPago.FORMAPAGO
            .DESCRIPCION = tDescripcion.Text.Trim
            .ACTIVO = True
        End With
        FormaPagoBL.Save(fpago)
        If IsNothing(fpago) Then
            Util.Mensaje("Error al Guardar Forma de Pago")
        Else
            Util.Mensaje("Forma de Pago Guardada Correctamente")
            Titulo()
            Limpiar()
        End If
    End Sub

    Private Sub bLimpiar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bLimpiar.Click
        Limpiar()

    End Sub

    Private Sub bEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bEliminar.Click
        If Not IsNothing(_FPago) Then
            If FormaPagoBL.Delete(_FPago.FORMAPAGO) Then
                Util.Mensaje("Forma de Pago Eliminada Correctamente")
                Limpiar()
            Else
                Util.MensajeError("Error al Elimiar Forma de Pago")
            End If
            Titulo()
        End If
    End Sub

    Private Sub lsFormasPago_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsFormasPago.SelectedIndexChanged
        If IsNumeric(lsFormasPago.SelectedValue) AndAlso lsFormasPago.SelectedIndex > -1 Then
            _FPago = FormaPagoBL.SearchById(lsFormasPago.SelectedValue)
            cargarFPago()
        End If
    End Sub

    Private Sub tBuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tBuscar.TextChanged
        If tBuscar.TextLength > 0 Then
            lsFormasPago.SelectedIndex = lsFormasPago.FindString(tBuscar.Text.Trim)
        End If
    End Sub

    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        Dispose()
    End Sub

    Private Sub tDescripcion_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tDescripcion.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub tBuscar_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tBuscar.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub

    Private Sub lsFormasPago_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles lsFormasPago.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            Util.Siguiente()
        End If
    End Sub
End Class